# #!/bin/bash

# # This script is used to deploy ceph cluster.

# CUR_DIR="$( cd "$( dirname "$(readlink -f "$0")" )" && pwd )"

# source $CUR_DIR/../cluster_IP.conf

# ORG_ID=0 # bc-storage cluster org id, which is globally unique, like 0-3
# OBJ_SIZE_TO_FABRIC=$(echo "4*1024*1024" | bc) # byte, default 4MB to test redundancy
# CEPH_POOL_NAME="testbench"

# help() {
#     echo "Usage:"
#     echo "    ./deploy_ceph.sh -i/--org_id <org_id> -p/--performance <true|false>"
#     echo "        -i, --org_id: bc-storage cluster org id, which is globally unique, like 0-3"
#     echo "        -p, --performance: whether to test performance, default false"
# }

# if [ $# -gt 5 ]; then
#     help
#     exit -1
# fi

# while [ $# -ge 1 ]; do
# case $1 in
#     -i | --org_id)
#         [ -z "$2" ] && help
#         ORG_ID=$2
#         shift
#         ;;
#     -p | --performance)
#         if [ "$2" == true ]; then
#             OBJ_SIZE_TO_FABRIC=$(echo "4*1024" | bc) # byte, 4KB to test performance
#         fi
#         shift
#         ;;
#     *)
#         help
#         exit -1
# esac
# shift
# done

# CEPH_NAME="ceph-demo-$ORG_ID"
# CEPH_SRC_DIR="$HOME/ssd/build-ceph/"

# KAFKA_IP=${KAFKA_IPs[$ORG_ID]}
# KAFKA_PORT=$(($KAFKA_PORT_BASE+$ORG_ID))
# ZOOKEEPER_PORT=$(($ZOOKEEPER_PORT_BASE+$ORG_ID))


# # clone ceph source code from gitee
# check_ceph_src() {
#     if [ ! -d $CEPH_SRC_DIR ]; then
#         mkdir $CEPH_SRC_DIR
#     fi
#     cd $CEPH_SRC_DIR
#     if [ -d $CEPH_SRC_DIR/$CEPH_NAME ]; then
#         echo "$CEPH_NAME exist"
#         return 0
#     fi

#     git clone https://gitee.com/czy233/ceph-demo.git $CEPH_SRC_DIR/$CEPH_NAME
#     if [ $? -ne 0 ]; then
#         echo "git clone gitee.com/czy233/ceph-demo error"
#         exit
#     fi
#     cd $CEPH_SRC_DIR/$CEPH_NAME 
#     git checkout fourOrgs
#     echo "SHOULD build ceph first!!!"
#     exit
# }

# # check existence of docker image
# check_ceph_docker() {
#     # judge whether docker image exists
#     exist=$(docker images | grep "ceph-dev-docker")
#     if [ $? -ne 0 ]; then
#         echo "docker build ceph-dev-docker"
#         cd $CUR_DIR
#         docker build --network=host -t ceph-dev-docker .
#         if [ $? -ne 0 ]; then
#             echo "docker build ceph-dev-docker error"
#             exit
#         fi
#     else
#         echo "docker image ceph-dev-docker exist"
#     fi
#     running=$(docker ps | grep "$CEPH_NAME")
#     if [ $? -ne 0 ]; then
#         echo "docker $CEPH_NAME is not running"
#         exit
#     else
#         echo "docker container $CEPH_NAME is running"
#     fi
# }

# # start ceph cluster
# start_ceph() {
#     echo "start ceph cluster"
#     local FABRIC_SDK_IP=${FABRIC_SDK_IPs[$ORG_ID]}
#     local FABRIC_SDK_PORT=$(($FABRIC_SDK_PORT_BASE+$ORG_ID))
#     local KAFKA_IP=${KAFKA_IPs[$ORG_ID]}
#     local KAFKA_PORT=$(($KAFKA_PORT_BASE+$ORG_ID))
#     docker exec $CEPH_NAME zsh -c "
#         source ~/.zshrc && \
#         sudo rm -rf /etc/ceph && \
#         sudo mkdir /etc/ceph && \
#         sudo chown -R ${HOST_USER}:${HOST_USER} /etc/ceph && \
#         cd $CEPH_SRC_DIR/$CEPH_NAME/build && \
#         ../src/stop.sh && \
#         rm -rf out dev && \
#         MON=1 MDS=1 MGR=1 ../src/vstart.sh --smallmds --new -X --localhost --msgr1 --without-dashboard \
#             -c $ORG_ID \
#             --fabric_kafka_brokers $KAFKA_IP:$KAFKA_PORT \
#             --fabric_kafka_topic $KAFKA_TOPIC_USED_BY_CEPH \
#             --fabric_sdk_addr $FABRIC_SDK_IP:$FABRIC_SDK_PORT \
#             --size_of_object_put_to_fabric $OBJ_SIZE_TO_FABRIC && \
#         ceph osd pool create $CEPH_POOL_NAME 32 32 && \
#         ceph osd pool application enable $CEPH_POOL_NAME rgw && \
#         ceph -s
#     "
#     echo "start ceph cluster done"
# }

# main() {
#     check_ceph_src
#     check_ceph_docker
#     # set -x
#     start_ceph
# }

# main

#!/bin/bash

# This script is used to deploy ceph cluster.

CUR_DIR="$( cd "$( dirname "$(readlink -f "$0")" )" && pwd )"

source $CUR_DIR/../cluster_IP.conf

ORG_ID=0 # bc-storage cluster org id, which is globally unique, like 0-3
OBJ_SIZE_TO_FABRIC=$(echo "4*1024*1024" | bc) # byte, default 4 MB to test redundancy
CEPH_POOL_NAME="testbench"

help() {
    echo "Usage:"
    echo "    ./deploy_ceph.sh -i/--org_id <org_id> -p/--performance <true|false>"
    echo "        -i, --org_id: bc-storage cluster org id, which is globally unique, like 0-3"
    # echo "        -p, --performance: whether to test performance, default false"
    echo "        -s, --size_of_object_put_to_fabric: size of object put to fabric, default 4 MB"
    exit -1
}

if [ $# -gt 7 ]; then
    help
    exit -1
fi

while [ $# -ge 1 ]; do
case $1 in
    -i | --org_id)
        [ -z "$2" ] && help
        ORG_ID=$2
        shift
        ;;
    # -p | --performance)
    #     if [ "$2" == true ]; then
    #         OBJ_SIZE_TO_FABRIC=$(echo "4*1024" | bc) # byte, 4KB to test performance
    #     fi
    #     shift
    #     ;;
    -s | --size_of_object_put_to_fabric)
        [ -z "$2" ] && help
        OBJ_SIZE_TO_FABRIC=$2
        shift
        ;;
    *)
        help
        exit -1
esac
shift
done

CEPH_NAME="ceph-demo-$ORG_ID"
CEPH_SRC_DIR="$HOME/ssd/build-ceph/"

KAFKA_IP=${KAFKA_IPs[$ORG_ID]}
KAFKA_PORT=$(($KAFKA_PORT_BASE+$ORG_ID))
ZOOKEEPER_PORT=$(($ZOOKEEPER_PORT_BASE+$ORG_ID))


# clone ceph source code from gitee
check_ceph_src() {
    if [ ! -d $CEPH_SRC_DIR ]; then
        mkdir $CEPH_SRC_DIR
    fi
    cd $CEPH_SRC_DIR
    if [ -d $CEPH_SRC_DIR/$CEPH_NAME ]; then
        echo "$CEPH_NAME exist"
        return 0
    fi

    git clone https://gitee.com/czy233/ceph-demo.git $CEPH_SRC_DIR/$CEPH_NAME
    if [ $? -ne 0 ]; then
        echo "git clone gitee.com/czy233/ceph-demo error"
        exit
    fi
    cd $CEPH_SRC_DIR/$CEPH_NAME 
    git checkout fourOrgs
    echo "SHOULD build ceph first!!!"
    exit
}

# check existence of docker image
check_ceph_docker() {
    # judge whether docker image exists
    exist=$(docker images | grep "ceph-dev-docker")
    if [ $? -ne 0 ]; then
        echo "docker build ceph-dev-docker"
        cd $CUR_DIR
        docker build --network=host -t ceph-dev-docker .
        if [ $? -ne 0 ]; then
            echo "docker build ceph-dev-docker error"
            exit
        fi
    else
        echo "docker image ceph-dev-docker exist"
    fi
    running=$(docker ps | grep "$CEPH_NAME")
    if [ $? -ne 0 ]; then
        echo "docker $CEPH_NAME is not running"
        exit
    else
        echo "docker container $CEPH_NAME is running"
    fi
}

# start ceph cluster
start_ceph() {
    echo "start ceph cluster"
    local FABRIC_SDK_IP=${FABRIC_SDK_IPs[$ORG_ID]}
    local FABRIC_SDK_PORT=$(($FABRIC_SDK_PORT_BASE+$ORG_ID))
    local KAFKA_IP=${KAFKA_IPs[$ORG_ID]}
    local KAFKA_PORT=$(($KAFKA_PORT_BASE+$ORG_ID))
    docker exec $CEPH_NAME zsh -c "
        source ~/.zshrc && \
        sudo rm -rf /etc/ceph && \
        sudo mkdir /etc/ceph && \
        sudo chown -R ${HOST_USER}:${HOST_USER} /etc/ceph && \
        cd $CEPH_SRC_DIR/$CEPH_NAME/build && \
        ../src/stop.sh && \
        rm -rf out dev && \
        MON=1 ../src/vstart.sh --smallmds --new -X --localhost --msgr1 --without-dashboard \
            -c $ORG_ID \
            --fabric_kafka_brokers $KAFKA_IP:$KAFKA_PORT \
            --fabric_kafka_topic $KAFKA_TOPIC_USED_BY_CEPH \
            --fabric_sdk_addr $FABRIC_SDK_IP:$FABRIC_SDK_PORT \
            --size_of_object_put_to_fabric $OBJ_SIZE_TO_FABRIC && \
        ceph config set global mon_warn_on_pool_no_redundancy false && \
        ceph osd pool create $CEPH_POOL_NAME 32 32 && \
        ceph osd pool application enable $CEPH_POOL_NAME rgw && \
        # radosgw-admin user create --uid='testuser' --display-name='First User' > ./secret_$ORG_ID.json && \
        ceph -s
    "
    echo "start ceph cluster done"
}

main() {
    check_ceph_src
    check_ceph_docker
    # set -x
    start_ceph
}

main